package com.sebscape.sebcms.pages.domain;

import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Unique;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.sebscape.sebcms.persistence.domain.IPersistedObject;
import com.sebscape.sebcms.persistence.domain.IUpdateable;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
@Unique(name="PAGE_UNIQUE_IDX", members={"customUrl"})
public class SiteMapEntry implements IPersistedObject, IUpdateable<SiteMapEntry>{
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;

	@Persistent
	private String name;
	
	@Persistent
	private String description;
	
	@Persistent
	private Key pageKey;
	
	@Persistent
	private Key parentSiteMapEntryKey;
	
	@Persistent
	private List<Key> childSiteMapEntryKeyList;
	
	
	/**
	 * 
	 */
	public SiteMapEntry() {
		super();
	}

	/**
	 * @param pageKey
	 * @param parentKey
	 */
	public SiteMapEntry(String name, String description, Key pageKey, Key parentSiteMapEntryKey) {
		super();
		this.pageKey = pageKey;
		this.parentSiteMapEntryKey = parentSiteMapEntryKey;
	}

	/**
	 * @param page
	 * @param parentKey
	 */
	public SiteMapEntry(Page page, Key parentSiteMapEntryKey) {
		this( page.getName(), 
				page.getDescription(),
				KeyFactory.createKey( "Page", page.getId() ), 
				parentSiteMapEntryKey);
	}
	
	/**
	 * @param page
	 * @param parentPage
	 */
	public SiteMapEntry(Page page, SiteMapEntry parentSiteMapEntry) {
		this( page, KeyFactory.createKey( "SiteMapEntry", parentSiteMapEntry.getId() ) );
	}
	
	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the pageKey
	 */
	public Key getPageKey() {
		return pageKey;
	}

	/**
	 * @param the pageKey to set
	 */
	public void setPageKey(Key pageKey) {
		this.pageKey = pageKey;
	}

	/**
	 * @return the parentKey
	 */
	public Key getParentSiteMapEntryKey() {
		return parentSiteMapEntryKey;
	}

	/**
	 * @param parentKey the parentKey to set
	 */
	public void setParentSiteMapEntryKey(Key parentSiteMapEntryKey) {
		this.parentSiteMapEntryKey = parentSiteMapEntryKey;
	}

	/**
	 * @return the pageKeyList
	 */
	public List<Key> getChildSiteMapEntryKeyList() {
		return childSiteMapEntryKeyList;
	}
	
	/**
	 * @param the pageKeyList to set
	 */
	public void setChildSiteMapEntryKeyList(List<Key> childSiteMapEntryKeyList) {
		this.childSiteMapEntryKeyList = childSiteMapEntryKeyList;
	}
	
	@Override
	public void update(SiteMapEntry newValues) {
		this.name = newValues.getName();
		this.description = newValues.getDescription();

		this.pageKey = newValues.getPageKey();
		this.parentSiteMapEntryKey = newValues.getParentSiteMapEntryKey();
		this.childSiteMapEntryKeyList = newValues.getChildSiteMapEntryKeyList();
	}
		
}
